package netty.demo_03_HTTP;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * 快速入门实例-HTTP服务
 * 服务端
 *
 * @author chenyuan
 * @date 2020/11/7 15:23
 */
public class HttpServer {
	public static void main(String[] args) throws Exception {
		EventLoopGroup bossGroup = new NioEventLoopGroup(1);
		EventLoopGroup workerGroup = new NioEventLoopGroup(8);

		try {
			// 创建 服务端 启动对象，并配置参数
			ServerBootstrap serverBootstrap = new ServerBootstrap();
			serverBootstrap.group(bossGroup, workerGroup)
					.channel(NioServerSocketChannel.class)
					// 这里没有直接没有直接 new 一个接口，而是引入自己写的一个实现类
					// 使用自己写的 ServerInitializer 完成初始化
					.childHandler(new HttpServerInitializer());
			System.out.println("服务器准备好了……");

			ChannelFuture channelFuture = serverBootstrap.bind(6660).sync();
			channelFuture.channel().closeFuture().sync();
		} finally {
			bossGroup.shutdownGracefully();
			workerGroup.shutdownGracefully();
		}
	}
}
